﻿var prizeStateJsons;
var pageSize = 15;
$(document).ready(function () {
    $.ajax({
        type: "post",
        data: { action: "GetPrizeState" },
        dataType: "json",
        url: "../../Ajax/PrizeRecordAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                prizeStateJsons = data.json;
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });
    $("#btnSele").click(function () {
        PageClickFunc(1);
    });
    PageClickFunc(1);


    //批量更新
    $("#btnBatchEdit").click(function () {

        var content = "<table><tr><td>开始时间： <input type=\"text\" id=\"txtEditStartTime\" onfocus=\"WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})\"class=\"Wdate\" /></td></tr>"
        content += "<tr><td>结束时间： <input type=\"text\" id=\"txtEditEndTime\" onfocus=\"WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})\"class=\"Wdate\" /></td></tr>"
        content += "<tr><td>系统账号：<input type=\"text\" id='txtUserAccount'/></td></tr>"
        content += "<tr><td>状态：<select id='selprizeState1'>" + InitSelectState() + "</select></td></tr>"
        content += "<tr><td>替换状态：<select id='selprizeState2'>" + InitSelectState() + "</select></td></tr></table>"

        $.dialog({
            title: "批量替换",
            content: content,
            button: [
                    {
                        name: "提交",
                        callback: function () {
                            $.ajax({
                                type: "post",
                                data: { action: "BatchEdit", startTime: $("#txtEditStartTime").val(), endTime: $("#txtEditEndTime").val(), state1: $("#selprizeState1").val(), state2: $("#selprizeState2").val(), userAccount: $("#txtUserAccount").val() },
                                //dataType: "json",
                                url: "../../Ajax/PrizeRecordAjax.ashx",
                                beforeSend: function () {
                                },
                                success: function (data) {
                                    if (data) {
                                        $.dialog.tips("批量更新成功！", 1);
                                    }
                                },
                                complete: function () {
                                },
                                error: function () {
                                }
                            });
                        }
                    },
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
            lock: true,
            drag: false
        });



    })
})
PageClickFunc = function (pageclickednumber) {
    InitPrizeRecord(pageclickednumber);
}
function InitPrizeRecord(pageclickednumber) {

    var txtCondition = $("#txtCondition").val();
    var txtSelectID = $("#selectByType").val();
    var txtStartTime = $("#txtStartTime").val();
    var txtEndTime = $("#txtEndTime").val();
    $.ajax({
        type: "post",
        data: { action: "Select", pageIndex: pageclickednumber, pageSize: pageSize, state: txtCondition, selectByID: txtSelectID, StartTime: txtStartTime, EndTime: txtEndTime },
        dataType: "json",
        url: "../../Ajax/PrizeRecordAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                var json = data.json;
                var tbody = "";
                var pageCount = data.flag;
                $.each(json, function (k, v) {
                    tbody += "<tr>"
                    tbody += "<td><lable name='lblOrderAccount' >";
                    tbody += v.OrderAccount;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblIMSI' id='" + v.ID + "'>";
                    tbody += v.IMSI;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblMac'>";
                    tbody += v.Mac;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lbUserAccount'>";
                    tbody += v.UserAccount;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lbluiname'>";
                    tbody += v.Uiname;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblpname'>";
                    tbody += v.Pname;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblCreateTime' >";
                    tbody += v.CreateTime;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblEditTime' >";
                    tbody += v.EditTime;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblUserName' >";
                    tbody += decodeURIComponent(v.UserName);
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblUserQQ' >";
                    tbody += v.UserQQ;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblUserPhone' >";
                    tbody += v.UserPhone;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblUserAddr' >";
                    tbody += decodeURIComponent(v.UserAddr);
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblstateInfo' id='" + v.StateInfo + "' >";
                    tbody += v.StateInfoName;
                    tbody += "</label></td>";
                    tbody += "<td>"
                    tbody += "<input type='button' onclick='Update(this)' value='更新'>"
                    tbody += "</td>";
                    tbody += "</tr>"
                })
                $("#tb1").empty().append(tbody);
                $("#pager").pager({ pagenumber: pageclickednumber, pagecount: Math.ceil(pageCount / pageSize), buttonClickCallback: PageClickFunc });
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });

}
function InitByChange() {
    var txtSelectID = $("#selectByType>option:selected").attr("id");
    if (txtSelectID != 1) {
        $("#txtCondition").show();
        $("#txtStartTime").hide();
        $("#txtEndTime").hide();
    }
    else {
        $("#txtCondition").hide();
        $("#txtStartTime").show();
        $("#txtEndTime").show();
    }
}
function InitSelectState(value) {
    var sel = "";
    $.each(prizeStateJsons, function (k, v) {
        if ((value == null && k == 0) || v.ID == value)
            sel += "<option value='" + v.ID + "' selected='selected'>" + v.NAME + "</option>";
        else
            sel += "<option value='" + v.ID + "'>" + v.NAME + "</option>";
    })
    return sel;
}
function Update(contr) {
    var lbl = $(contr).parent().parent().find("lable[name=lblIMSI]").first();
    Edit(lbl);
}
function Edit(lbl) {
    //lbl 是判断是添加还是修改  添加为null
    var sortId = $(lbl).parent().parent().find("lable[name=lblIMSI]").attr("id");
    var data = new Date();
    var stateType = $(lbl).parent().parent().find("lable[name=lblstateInfo]").attr("id");
    var content = "";
    if (lbl != null) {
        //        content = "<table><tr><td>状态：<input type='text' id='txtCondition' value='" + $(lbl).parent().parent().find("lable[name=lblstateInfo]").first().text() + "' style='width:200px' /></td></tr></table>"
        content = "<table><tr><td>状态：<select id='selprizeState'>" + InitSelectState(stateType) + "</select></td></tr></table>"
    }
    else { }

    $.dialog({
        title: "奖品记录",
        content: content,
        button: [
                    {
                        name: "提交",
                        callback: function () {
                            AddMould(lbl, $("#selprizeState"), sortId);
                        }
                    },
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
        lock: true,
        drag: false
    });
}

function AddMould(contr, sortNum, sortId) {
    var objectId = sortId;
    // var state = $(sortNum).val();
    var state = $(sortNum).parent().parent().parent().find("#selprizeState").val();
    var stateName = $(sortNum).parent().parent().parent().find("#selprizeState>option:selected").text();
    var OrderAccount = $(contr).parent().parent().find("lable[name=lblOrderAccount]").text();
    var IMSI = $(contr).parent().parent().find("lable[name=lblIMSI]").text();
    var Mac = $(contr).parent().parent().find("lable[name=lblMac]").text();
    var Uiname = $(contr).parent().parent().find("lable[name=lblUiname]").text();
    var Pname = $(contr).parent().parent().find("lable[name=lblPname]").text();
    var CreateTime = $(contr).parent().parent().find("lable[name=lblCreateTime]").text();
    var EditTime = $(contr).parent().parent().find("lable[name=lblEditTime]").text();
    var UserName = $(contr).parent().parent().find("lable[name=lblUserName]").text();
    var UserQQ = $(contr).parent().parent().find("lable[name=lblUserQQ]").text();
    var UserPhone = $(contr).parent().parent().find("lable[name=lblUserPhone]").text();
    var UserAddr = $(contr).parent().parent().find("lable[name=lblUserAddr]").text();

    var data = contr == null ? {} :
              { action: "Update", id: objectId, state: state };
    $.ajax({
        type: "post",
        data: data,
        async: true,
        url: "../../Ajax/PrizeRecordAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            $.dialog.tips("更新成功！", 1);
            $(contr).parent().parent().find("lable[name=lblOrderAccount]").first().text(OrderAccount);
            $(contr).parent().parent().find("lable[name=lblIMSI]").first().text(IMSI);
            $(contr).parent().parent().find("lable[name=lblMac]").first().text(Mac);
            $(contr).parent().parent().find("lable[name=lblUiname]").first().text(Uiname);
            $(contr).parent().parent().find("lable[name=lblPname]").first().text(Pname);
            $(contr).parent().parent().find("lable[name=lblCreateTime]").first().text(CreateTime);
            $(contr).parent().parent().find("lable[name=lblEditTime]").first().text(UserName);
            $(contr).parent().parent().find("lable[name=lblUserName]").first().text(UserQQ);
            $(contr).parent().parent().find("lable[name=lblUserQQ]").first().text(UserPhone);
            $(contr).parent().parent().find("lable[name=lblUserPhone]").first().text(UserAddr);
            $(contr).parent().parent().find("lable[name=lblUserAddr]").first().text(UserAddr);
            $(contr).parent().parent().find("lable[name=lblstateInfo]").first().text(stateName);
        },
        complete: function () {
        },
        error: function () {
        }
    })
}
PageClickFuncs = function (sortId, selectById, txtStartTime, txtEndTime, pageclickednumber) {
    AddMouldState(sortId, selectById, txtStartTime, txtEndTime, pageclickednumber);
}
function AddMouldState(sortId, selectById, txtStartTime, txtEndTime, pageclickednumber) {
    var data = sortId == null ? {} :
              { action: "Select", pageIndex: pageclickednumber, pageSize: pageSize, state: sortId, selectByID: selectById, txtStartTime: txtStartTime, txtEndTime: txtEndTime };
    $.ajax({
        type: "post",
        data: data,
        dataType: "json",
        url: "../../Ajax/PrizeRecordAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                var json = data.json;
                var tbody = "";
                var pageCount = data.flag;
                $.each(json, function (k, v) {
                    tbody += "<tr>"
                    tbody += "<td><lable name='lblOrderAccount' >";
                    tbody += v.OrderAccount;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblIMSI' id='" + v.ID + "'>";
                    tbody += v.IMSI;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblMac'>";
                    tbody += v.Mac;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lbluiname'>";
                    tbody += v.Uiname;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblpname'>";
                    tbody += v.Pname;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblCreateTime' >";
                    tbody += v.CreateTime;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblEditTime' >";
                    tbody += v.EditTime;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblUserName' >";
                    tbody += v.UserName;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblUserQQ' >";
                    tbody += v.UserQQ;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblUserPhone' >";
                    tbody += v.UserPhone;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblUserAddr' >";
                    tbody += v.UserAddr;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblstateInfo' id='" + v.StateInfo + "' >";
                    tbody += v.StateInfoName;
                    tbody += "</label></td>";
                    tbody += "<td>"
                    tbody += "<input type='button' onclick='Update(this)' value='更新'>"
                    tbody += "</td>";
                    tbody += "</tr>"
                })
                $("#tb1").empty().append(tbody);
                $("#pager").pager({ pagenumber: pageclickednumber, pagecount: Math.ceil(pageCount / pageSize), buttonClickCallback: PageClickFuncs });
            }
        },
        complete: function () {
        },
        error: function () {
        }
    })
}